package com.multithreading.chart1.ThreadLocal;

/**
 * @author chenqb
 * @project_name: java-multithreading
 * @package_name: com.cqb.ThreadLocal
 * @ClassName: TestThreadLocal
 * @description: 1.threadlocal不支持继承，那么让子线程能够访问父线程的本地变量可以使用inheritableThreaLocal
 * 2.每个线程的本地变量存放在线程自己的内存变量threadLocals中，如果当前线程一直不消亡，这些变量一直存放在内存中，可能会造成内存溢出。
 * Author by cqb, Date on 2020/7/12，7:31
 * PS: Not easy to write code, please indicate.
 */
public class TestThreadLocal {
    //1.创建线程变量
    public static ThreadLocal<String> threadLocal = new ThreadLocal<String>();
    //2.使用InheritableThreadLocal让子线程也能获取到父线程中设置的变量
//    public static ThreadLocal<String> threadLocal =new InheritableThreadLocal<String>(); list项目  item任务 item_order任务订单

    public static void main(String[] args) {
//    2.设置线程变量
       threadLocal.set("hello world");
//    3.启动子线程
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                //    4.子线程输出线程变量的值
                System.out.println("thread:" + threadLocal.get());
            }
        });
        thread.start();
//    5.主线程输出线程变量的值
        System.out.println("main:" + threadLocal.get());
        threadLocal.remove();
        System.out.println("main:" + threadLocal.get());

    }


}

